package com.scgl.factory.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.scgl.factory.domain.Factory;
import com.scgl.factory.domain.FactoryVo;
import com.scgl.factory.service.IFactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author mobai
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/factory")
public class FactoryController extends BaseController
{
    @Autowired
    private IFactoryService factoryService;

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("factory:factory:list")
    @GetMapping("/list")
    public TableDataInfo list(Factory factory)
    {
        startPage();
        List<Factory> list = factoryService.selectFactoryList(factory);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("factory:factory:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Factory factory)
    {
        List<Factory> list = factoryService.selectFactoryList(factory);
        ExcelUtil<Factory> util = new ExcelUtil<Factory>(Factory.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @RequiresPermissions("factory:factory:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(factoryService.selectFactoryById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @RequiresPermissions("factory:factory:add")
    @PostMapping
    public AjaxResult add(@RequestBody Factory factory) {
        factory.setCreateDate(new Date());
        factory.setCreateName("张三");
        return toAjax(factoryService.insertFactory(factory));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("factory:factory:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody Factory factory)
    {
        return toAjax(factoryService.updateFactory(factory));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("factory:factory:remove")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(factoryService.deleteFactoryByIds(ids));
    }


    // 构建树结构数据
    @RequiresPermissions("factory:factory:listTreeData")
    @GetMapping("/listTreeData")
    public AjaxResult listTreeData()
    {
        List<Factory> list = factoryService.buildFactoryTree();
        return AjaxResult.success(list);
    }


    @RequiresPermissions("factory:factory:getFactoryList")
    @GetMapping("getFactoryList/{id}")
    public AjaxResult getFactoryList(@PathVariable("id") Long id){
        List<Factory> list = factoryService.selectFactoryLinkageList(id);
        return AjaxResult.success(list);
    }


}
